#include "config_file.h"

int load_config_file( const char *fname )
{
	FILE *f;
	int file_size = 0;
	int i;
	config_file_text = (char *)malloc(MAX_INPUT_SIZE);
	if (config_file_text == NULL)
	{
		printf("Cannot allocate memory for file\n");
		return -1;
	}
	memset( config_file_text, 0, MAX_INPUT_SIZE);
	
	f = fopen( fname, "r");
	if (f == NULL)
	{
		printf("Cannot open file %s\n", fname);
		return -1;
	}
	file_size = fread( config_file_text, 1 , MAX_INPUT_SIZE, f);
	fclose( f );
	
	config_root = json_loads( config_file_text, 0, &config_file_error);
	free( config_file_text );
	config_file_text = NULL;
	if (!config_root)
	{
		printf("Error in json file on line: %d: %s\n", config_file_error.line, config_file_error.text);
		return -1;
	}
	
	//config grid
	config_value = json_object_get( config_root, "grid");
	if ( json_is_boolean( config_value) )
	{
		if ( json_is_true( config_value ) )
		{
			v_grid = TRUE;
		}
		if ( json_is_false( config_value ) )
		{
			v_grid = FALSE;
		}
	}
	//config color
	config_value = json_object_get( config_root, "color");
	if ( json_is_string( config_value ) )
	{
		const char *tmp;
		tmp = json_string_value( config_value );
		if (strlen( tmp ) == 6)
		{
			v_color = strtoul( tmp, NULL, 16 );
		}
	}
	//config month
	config_value = json_object_get( config_root, "month");
	if ( json_is_boolean( config_value) )
	{
		if ( json_is_true( config_value ) )
		{
			v_month = TRUE;
		}
		if ( json_is_false( config_value ) )
		{
			v_month = FALSE;
		}
	}
	//config year
	config_value = json_object_get( config_root, "year");
	if ( json_is_boolean( config_value ) )
	{
		if ( json_is_true( config_value ) )
		{
			v_year = TRUE;
		}
		if ( json_is_false( config_value ) )
		{
			v_year = FALSE;
		}
	}
	//config bg path
	config_value = json_object_get( config_root, "image" );
	if ( json_is_string( config_value ))
	{
		v_bg_path = json_string_value( config_value );
	}
	//config font path
	config_value = json_object_get( config_root, "font");
	if ( json_is_string( config_value ))
	{
		v_font_path = json_string_value( config_value );
	}
	
	//config font size
	config_value = json_object_get( config_root, "font-size");
	if (json_is_integer( config_value ) )
	{
		v_font_size = json_integer_value( config_value );
	}
	//config x position
	config_value = json_object_get( config_root, "x");
	if ( json_is_integer(config_value) )
	{
		v_pos_x = json_integer_value( config_value );
	}
	//config y position
	config_value = json_object_get( config_root, "y");
	if ( json_is_integer(config_value) )
	{
		v_pos_y = json_integer_value( config_value );
	}
	//config wdth
	config_value = json_object_get( config_root, "width");
	if ( json_is_integer(config_value) )
	{
		v_width = json_integer_value( config_value );
	}
	return 0;
}

void config_values_reset()
{
	v_grid = FALSE;
	v_color = 0x00000000;
	v_month = FALSE;
	v_bg_path = NULL;
	v_font_path = NULL;
	v_pos_x = 0;
	v_pos_y = 0;
	v_font_size = 0;
	v_width = 0;
}


int config_gcollector()
{
	if ( config_file_text != NULL)
	{
		free(config_file_text);
	}
	json_decref( config_root );
}

